package pattern.strategy;

public class TestStrategy {
    public static void main(String[] args) {
        Context contextA = new Context(new StrategyA(60d));
        System.out.println(contextA.getResult());
        Context contextB = new Context(new StrategyB(60d, 0.8d));
        System.out.println(contextB.getResult());
        Context contextC = new Context(new StrategyB(60d, 10d));
        System.out.println(contextC.getResult());
    }
}

/**
 * 上下文
 */
 class Context {

    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public Double getResult() {
        return strategy.getResult();
    }
}
/**
 * 策略接口
 */
 interface Strategy {

    public Double getResult();
}
/**
 * （具体策略类）原价策略
 */
 class StrategyA implements Strategy{

    private Double money;

    public StrategyA(Double money) {
        this.money = money;
    }
    @Override
    public Double getResult() {
        return money;
    }
}
/**
 * （具体策略类）折扣策略
 */
 class StrategyB implements Strategy{

    private Double money;

    private Double discount;

    public StrategyB(Double money, Double discount) {
        this.money = money;
        this.discount = discount;
    }

    @Override
    public Double getResult() {

        return money * discount;
    }
}
/**
 * （具体策略类）满减策略
 */
 class StrategyC implements Strategy{

    private Double money;

    private Double subMoney;

    public StrategyC(Double money, Double subMoney) {
        this.money = money;
        this.subMoney = subMoney;
    }
    @Override
    public Double getResult() {

        if (money > subMoney) {
            return money - subMoney;
        }
        return money;
    }
}

